package com.hzhk.happyrent.ui.activity.bill;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.CodeModel;
import com.hzhk.happyrent.bean.bill.ProductPeriodsBean;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.adapter.DataNullAdapter;
import com.hzhk.happyrent.ui.adapter.bill.ProductPeriodsAdapter;
import com.hzhk.happyrent.utils.ListUtils;
import com.hzhk.happyrent.utils.MyPerference;
import com.tamic.novate.Throwable;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BillActivity extends AppCompatActivity {

    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.pay_total)
    TextView payTotal;
    @BindView(R.id.lv_bill)
    ListView lvBill;
    @BindView(R.id.pay_need)
    TextView payNeed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        activityTitlebarCenter.setText("交租");
        initData();
    }

    private void initData() {
        Map<String, Object> params = new HashMap<>();
//        params.put("order_id", getIntent().getStringExtra("order_id"));
        params.put("user_id", MyPerference.getInstance().getUserId());
        HttpManager.getInstance().get(API.GET_PERIODS, params, new MyStringResponse(this) {
            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                CodeModel codeModel = new Gson().fromJson(response, CodeModel.class);
                if (0 == codeModel.getErrCode()) {
                    ProductPeriodsBean productPeriodsBean = new Gson().fromJson(response, ProductPeriodsBean.class);
                    if (!ListUtils.isEmpty(productPeriodsBean.getData().getOrders())) {
                        payTotal.setText("￥"+productPeriodsBean.getData().getCountNeedPayMoney());
                        payNeed.setText(productPeriodsBean.getData().getCountNeedPayPeriod()+"期");
                        ProductPeriodsAdapter productPeriodsAdapter = new ProductPeriodsAdapter(BillActivity.this);
                        productPeriodsAdapter.setList(productPeriodsBean.getData().getOrders());
                        lvBill.setAdapter(productPeriodsAdapter);
                    } else {
                        lvBill.setAdapter(new DataNullAdapter(BillActivity.this));
                    }
                }
            }
        });
    }

    @OnClick(R.id.activity_titlebar_left)
    public void onViewClicked() {
        finish();
    }
}
